#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import pandas as pd
import datetime
from xpy3lib.XRetryableQuery import XRetryableQuery
from xpy3lib.XRetryableSave import XRetryableSave
from sicost.AbstractDPJob import AbstractDPJob
from xpy3lib.utils.XDateTimeUtils import XDateTimeUtils


class BZSJCB_0_Job(AbstractDPJob):
    """

    """

    data_type = None

    def __init__(self,
                 p_config=None,
                 p_db_conn_mpp=None,
                 p_db_conn_dbprod7=None,
                 p_db_conn_rds=None,
                 p_account=None,
                 p_account_period_start=None,
                 p_account_period_end=None,
                 p_data_type=None):

        super(BZSJCB_0_Job, self).__init__(p_config=p_config,
                                         p_db_conn_mpp=p_db_conn_mpp,
                                         p_db_conn_rds=p_db_conn_rds,
                                         p_db_conn_dbprod7=p_db_conn_dbprod7,
                                         p_unit=None,
                                         p_account=p_account,
                                         p_cost_center=None,
                                        p_account_period_start=p_account_period_start,
                                        p_account_period_end=p_account_period_end)
        self.data_type = p_data_type




        pass

    def do_execute(self):
        """
        """
        self.logger.info('BZBZCB_0_Job.do_execute')

        sql = " DELETE FROM BGTARAS1.T_ADS_FACT_SICB_BZSJCB_0 " \
              " WHERE ACCT = '%s' AND PRODUCE_TIME >=  '%s' AND PRODUCE_TIME<='%s'"%(self.account,self.account_period_start,self.account_period_end)

        self.logger.info(sql)
        self.db_conn_rds.execute(sql)





        sql = " SELECT " \
              " ACCT as ACCOUNT, " \
              " PROD_COILNO as MAT_NO, " \
              " SUM(ACT_OUTPUT_WT) AS MAT_ACT_WT, " \
              " PRODUCE_TIME as WORK_TIME," \
              " COATING_TYPE AS PACK_TYPE_CODE," \
              " CASE" \
              " WHEN COST_CENTER IN ('MIDN','MIDM','MIDY','MIDZ')  " \
              " THEN '无取向硅钢' " \
              " WHEN COST_CENTER IN ('MICX','MIHY','MIHZ','MICZ') " \
              " THEN '取向硅钢' " \
              " END AS QS_NAME, " \
              " SUM(PROC_ON_AMT) AS BZZC, " \
              " SUM(UNIT_PROC_ON_AMT) AS BZCB " \
              " from BGTARAS1.T_ADS_FACT_SICB_CA0001 " \
              " where DATA_TYPE='%s' AND ACCT = '%s' AND COST_CENTER IN ('MIDN','MIDM','MIDY','MIDZ','MICX','MIHY','MIHZ','MICZ') " \
              " AND PRODUCE_TIME >=  '%s' AND PRODUCE_TIME<='%s' AND COST_SUBJECT = '65000' " \
              " GROUP BY ACCT,PROD_COILNO,PRODUCE_TIME,COATING_TYPE, " \
              " CASE " \
              " WHEN COST_CENTER IN ('MIDN','MIDM','MIDY','MIDZ')  " \
              " THEN '无取向硅钢' " \
              " WHEN COST_CENTER IN ('MICX','MIHY','MIHZ','MICZ') " \
              " THEN '取向硅钢' " \
              " END " \
              " HAVING SUM(ACT_OUTPUT_WT) <> 0 " %(self.data_type,self.account,self.account_period_start,self.account_period_end)
        self.logger.info(sql)

        df0 = XRetryableQuery(p_db_conn=self.db_conn_rds, p_sql=sql, p_max_times=5).redo()
        success = df0.empty is False
        if success is False:
            return
        df0.columns = df0.columns.str.upper()


        df0.rename(columns={'ACCOUNT': 'ACCT'}, inplace=True)
        df0.rename(columns={'MAT_NO': 'PROD_COILNO'}, inplace=True)
        df0.rename(columns={'MAT_ACT_WT': 'MAT_ACT_WT'}, inplace=True)
        df0.rename(columns={'WORK_TIME': 'PRODUCE_TIME'}, inplace=True)
        df0.rename(columns={'PACK_TYPE_CODE': 'PACK_TYPE_CODE'}, inplace=True)
        df0.rename(columns={'QS_NAME': 'PROD_CAT'}, inplace=True)
        df0.rename(columns={'BZZC': 'OTHER_FEE'}, inplace=True)
        df0.rename(columns={'BZCB': 'COST_PACK'}, inplace=True)








        XRetryableSave(p_db_conn=self.db_conn_rds, p_table_name='T_ADS_FACT_SICB_BZSJCB_0', p_schema='BGTARAS1',
                       p_dataframe=df0,
                       p_max_times=5).redo()



        super(BZSJCB_0_Job, self).do_execute()
